package com.liangdemo.web.test;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Mr.Liang
 * @date 2022/4/28 9:32
 */

public class Text1Controller {
    public static List<User1> users1 = new ArrayList<>();

    static {
        users1.add(new User1(1, "JACK", 22));
        users1.add(new User1(2, "LILY", 21));
        users1.add(new User1(3, "LUCY", 24));
        users1.add(new User1(4, "TOM", 23));
        users1.add(new User1(5, "TOMAS", 25));
    }

    public static void main(String[] args) {
        // 题目一、将users集合中的元素按照年龄 age 从大到小的顺序排列
        List<User1> user1s = users1.stream().sorted(Comparator.comparing(User1::getAge).reversed()).collect(Collectors.toList());
        user1s.forEach(user1 -> {
            System.out.println(user1);
        });
        users1.sort(new Comparator<User1>() {
            @Override
            public int compare(User1 o1, User1 o2) {
                return o1.getAge() - o2.getAge();
            }
        });




        // 题目二、将users集合中所有元素的 id 取出，拼接成逗号分割的字符串，如：1,2,3,4,5
        List<Integer> list = new ArrayList<>();
        String  pinjie = "";
        for(User1 user:user1s){
            pinjie+=user.getId()+",";
        }
        System.out.println(pinjie);
        // 题目三、统计字符串 str 中字母 I 出现的次数
        String str = "ILOVECHINA";
        String[] a=str.split("");






    }


}

class User1 {

    private Integer id;

    private String name;

    private Integer age;

    public User1() {
    }

    public User1(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
